package cn.edu.cug.cs.gtl.apis.ui.projecttree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.tree.Tree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeMouseListener;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.Layer;
import cn.edu.cug.cs.gtl.protos.Map;
import cn.edu.cug.cs.gtl.protos.Project;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;

public class ProjectTreeMouseListener extends TreeMouseListener {

    public ProjectTreeMouseListener(Tree tree, AbstractApplication theApp) {
        super(tree, theApp);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     */
    @Override
    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     */
    @Override
    public void mouseReleased(MouseEvent e) {
        super.mouseReleased(e);
        if(SwingUtilities.isRightMouseButton(e)){
            ProjectTree tree= theApp.getMainFrame().getProjectPanel().getTreeComponent();
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            tree.setSelectionPath(selPath);
            if (selRow>-1){
                tree.setSelectionRow(selRow);
            }
            else{
                tree.setSelectionRow(0);
            }

            DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) tree.getCurrentSelectedNode();
            TreeNodeData userObject = (TreeNodeData) treeNode.getUserObject();
            Object refObject  =  userObject.getRefObject();

            if(refObject instanceof Project){
                JPopupMenu popupMenu = tree.getProjectNodePopupMenu();
                if(popupMenu==null || popupMenu.getComponentCount()==0){
                    popupMenu = ProjectTree.createDefaultPopupMenuForProjectNode();
                    tree.setProjectNodePopupMenu(popupMenu);
                }
                else
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
            else if(refObject instanceof Map){
                JPopupMenu popupMenu = tree.getMapNodePopupMenu();
                if(popupMenu==null || popupMenu.getComponentCount()==0){
                    popupMenu = ProjectTree.createDefaultPopupMenuForMapNode();
                    tree.setMapNodePopupMenu(popupMenu);
                }
                else
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
            else if(refObject instanceof Layer){
                JPopupMenu popupMenu = tree.getLayerNodePopupMenu();
                if(popupMenu==null || popupMenu.getComponentCount()==0){
                    popupMenu = ProjectTree.createDefaultPopupMenuForLayerNode();
                    tree.setLayerNodePopupMenu(popupMenu);
                }
                else
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
            else{
                JPopupMenu popupMenu = tree.getProjectNodePopupMenu();
                if(popupMenu==null || popupMenu.getComponentCount()==0){
                    popupMenu = ProjectTree.createDefaultPopupMenuForProjectNode();
                    tree.setProjectNodePopupMenu(popupMenu);
                }
                else
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     */
    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     */
    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     * @since 1.6
     */
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        super.mouseWheelMoved(e);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     * @since 1.6
     */
    @Override
    public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
    }

    /**
     * {@inheritDoc}
     *
     * @param e
     * @since 1.6
     */
    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
    }
}
